//
//  InsuranceBrokersViewController.m
//  XinKaiFa55like
//
//  Created by junseek on 2017/1/16.
//  Copyright © 2017年 55like lj. All rights reserved.
//

#import "InsuranceBrokersViewController.h"
#import "RHTableView.h"
#import "InsuranceBrokersTableViewCell.h"
#import "UserSetViewController.h"
#import "InsuranceBrokersContentViewController.h"
#import "ProductKeysView.h"

@interface InsuranceBrokersViewController ()<UITextFieldDelegate,ProductKeysViewDelegate>{
    UITextField *txtSarch;
    RHTableView *tabelViewC;
    
    UIView * viewTopBG;
    UIButton *btnAll;
//    UIButton *btn2;
//    UIButton *btn3;
//    UIButton *btn4;
    UIView *viewLine;
    NSString *strlevelid;
    
    ProductKeysView *productKey;
    
}
@end

@implementation InsuranceBrokersViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    strlevelid=@"";
    // Do any additional setup after loading the view.
    self.navView.frameHeight=108;
    [self.navView viewWithTag:104].hidden=YES;
    [self rightButton:nil image:@"headSet070116" sel:@selector(rightButtonClicked)];
    
    UIView *viewTxtBG=[[UIView alloc] initWithFrame:CGRectMake(10, 68, kScreenWidth-20, 30)];
    viewTxtBG.backgroundColor=[UIColor whiteColor];
    [self.navView addSubview:viewTxtBG];
    [[Utility Share] viewLayerRound:viewTxtBG borderWidth:1 borderColor:[UIColor clearColor]];
    
    txtSarch=[RHMethods textFieldlWithFrame:CGRectMake(10, 0, W(viewTxtBG)-10, 30) font:fontTitle color:rgbTitleColor placeholder:@"" text:@""];
    [viewTxtBG addSubview:txtSarch];
    [txtSarch setLeftView:[RHMethods imageviewWithFrame:CGRectMake(0, 0, 26, 30) defaultimage:@"searchIcon070116" contentMode:UIViewContentModeCenter] ];
    [txtSarch setLeftViewMode:UITextFieldViewModeAlways];
    [txtSarch setReturnKeyType:UIReturnKeySearch];
    txtSarch.delegate=self;
   
    float fty=108;
    if (!(self.userInfo && [self.userInfo isEqualToString:@"userReleased"])) {
        
        viewTopBG=[[UIView alloc] initWithFrame:CGRectMake(0, fty, kScreenWidth, 40)];
        [viewTopBG setBackgroundColor:[UIColor whiteColor]];
        [self.view addSubview:viewTopBG];
        
        NSArray *arrayL=[[Utility Share] arrayLevel];
        NSInteger count1=[arrayL count]>4?4:[arrayL count];
        float fw=(kScreenWidth-20.0)/(count1+1);
        btnAll=[RHMethods buttonWithFrame:CGRectMake(10, 0, fw, 40) title:@"所有" image:@"" bgimage:@""];
        [btnAll setTitleColor:rgbpublicColor forState:UIControlStateSelected];
        [btnAll setTitleColor:rgbTitleColor forState:UIControlStateNormal];
        [btnAll addTarget:self action:@selector(selectTypeButtonCLiked:) forControlEvents:UIControlEventTouchUpInside];
        [viewTopBG addSubview:btnAll];
        btnAll.tag=100;
        
        float fx=XW(btnAll);
        for (int i=0; i<count1; i++) {
            NSDictionary *dic=[arrayL objectAtIndex:i];
            UIButton * btnTemp=[RHMethods buttonWithFrame:CGRectMake(fx, Y(btnAll), fw, H(btnAll)) title:[dic valueForJSONStrKey:@"name"] image:@"" bgimage:@""];
            [btnTemp setTitleColor:rgbpublicColor forState:UIControlStateSelected];
            [btnTemp setTitleColor:rgbTitleColor forState:UIControlStateNormal];
            [btnTemp addTarget:self action:@selector(selectTypeButtonCLiked:) forControlEvents:UIControlEventTouchUpInside];
            [viewTopBG addSubview:btnTemp];
            btnTemp.tag=101+i;
            fx=XW(btnTemp);
        }
        
//        btn2=[RHMethods buttonWithFrame:CGRectMake(XW(btnAll), Y(btnAll), fw, H(btnAll)) title:@"A级" image:@"" bgimage:@""];
//        [btn2 setTitleColor:rgbpublicColor forState:UIControlStateSelected];
//        [btn2 setTitleColor:rgbTitleColor forState:UIControlStateNormal];
//        [btn2 addTarget:self action:@selector(selectTypeButtonCLiked:) forControlEvents:UIControlEventTouchUpInside];
//        [viewTopBG addSubview:btn2];
        
//        btn3=[RHMethods buttonWithFrame:CGRectMake(XW(btn2), Y(btnAll), fw, H(btnAll)) title:@"B级" image:@"" bgimage:@""];
//        [btn3 setTitleColor:rgbpublicColor forState:UIControlStateSelected];
//        [btn3 setTitleColor:rgbTitleColor forState:UIControlStateNormal];
//        [btn3 addTarget:self action:@selector(selectTypeButtonCLiked:) forControlEvents:UIControlEventTouchUpInside];
//        [viewTopBG addSubview:btn3];
//        
//        btn4=[RHMethods buttonWithFrame:CGRectMake(XW(btn3), Y(btnAll), fw, H(btnAll)) title:@"C级" image:@"" bgimage:@""];
//        [btn4 setTitleColor:rgbpublicColor forState:UIControlStateSelected];
//        [btn4 setTitleColor:rgbTitleColor forState:UIControlStateNormal];
//        [btn4 addTarget:self action:@selector(selectTypeButtonCLiked:) forControlEvents:UIControlEventTouchUpInside];
//        [viewTopBG addSubview:btn4];
//        
        
        [viewTopBG addSubview:[RHMethods imageviewWithFrame:CGRectMake(0,  H(viewTopBG)-0.5, kScreenWidth, 0.5) defaultimage:@"userLine"]];
        
        
        viewLine=[[UIView alloc]initWithFrame:CGRectMake(X(btnAll), H(viewTopBG)-2, W(btnAll), 2)];
        [viewLine setBackgroundColor:rgbpublicColor];
        [viewTopBG addSubview:viewLine];
        
        fty+=40;
    }
    
    
    tabelViewC=[[RHTableView alloc]initWithFrame:CGRectMake(0, fty, kScreenWidth, H(self.view)-fty)];
    [tabelViewC showRefresh:YES LoadMore:YES];
    tabelViewC.delegate = self;
    tabelViewC.dataSource = self;
    //    tabelViewC.delegate2=self;
    [tabelViewC setBackgroundColor:rgbGray];
    [tabelViewC setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [tabelViewC registerClass:[InsuranceBrokersTableViewCell class] forCellReuseIdentifier:@"InsuranceBrokersTableViewCell"];
    [tabelViewC setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
    
    [self.view addSubview:tabelViewC];
    
    if ([self.userInfo isEqualToString:@"search"]) {
        txtSarch.text=[self.otherInfo valueForJSONStrKey:@"txt"];
    }
    
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self refreshTableData];
 
    //注册监听键盘事件
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardDidShow:) name:UIKeyboardWillShowNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    
    
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark
-(void)handleKeyboardDidShow:(NSNotification *)notification{
    
    NSDictionary *info = [notification userInfo];
    CGRect keyboardFrame;
    [[info objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardFrame];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue].size;
    CGFloat distanceToMove = kbSize.height;
    NSLog(@"---->动态键盘高度:%f",distanceToMove);
    if (!productKey) {
        productKey=[[ProductKeysView alloc] init];
        [self.view addSubview:productKey];
        productKey.delegate=self;
        [productKey showAllArray:[[Utility Share] arrayProductKeys]];
    }
    productKey.frame=CGRectMake(0, 108, kScreenWidth, kScreenHeight-distanceToMove-108);
    [productKey show];
    
    [UIView animateWithDuration:0.3 animations:^{
        
    } completion:^(BOOL finished) {
        
    }];
}
- (void)handleKeyboardWillHide:(NSNotification *)notification
{
    [productKey hidden];
    [UIView animateWithDuration:0.3 animations:^{
        
    }completion:^(BOOL finished) {
        
    }];
}//
-(void)refreshTableData{
    tabelViewC.contentOffset=CGPointZero;
    if ((self.userInfo && [self.userInfo isEqualToString:@"userReleased"])) {
        tabelViewC.urlString=[NSString stringWithFormat:XKFproductsmypost,[[Utility Share] userId],[[Utility Share] userToken],[self.otherInfo valueForJSONStrKey:@"id"],txtSarch.text,@"%@",strlevelid];
    }else{
        tabelViewC.urlString=[NSString stringWithFormat:XKFproductslists,[[Utility Share] userId],[[Utility Share] userToken],[self.otherInfo valueForJSONStrKey:@"id"],txtSarch.text,@"%@",strlevelid,@"lists"];
    }
    
    [tabelViewC refresh];
}
#pragma mark button
-(void)selectTypeButtonCLiked:(UIButton *)btn{
    [txtSarch resignFirstResponder];
    if (btn.selected) {
        return;
    }
    for (UIView *viewt in [viewTopBG subviews]) {
        if ([viewt isKindOfClass:[UIButton class]]) {
            UIButton *btntt=(UIButton *)viewt;
            btntt.selected=NO;
        }
    }
//    btn4.selected=NO;
//    btn2.selected=NO;
//    btn3.selected=NO;
//    btnAll.selected=NO;
    btn.selected=YES;
    [UIView animateWithDuration:0.3 animations:^{
        viewLine.frame=CGRectMake(X(btn), Y(viewLine), W(viewLine), H(viewLine));
    }];
    if (btnAll.selected) {
       strlevelid=@"";
    }else {
        NSDictionary *dic=[[[Utility Share] arrayLevel] objectAtIndex:btn.tag-101];
        strlevelid=[dic valueForJSONStrKey:@"id"];
    }
    [self refreshTableData];
}
#pragma mark tableView
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return tabelViewC.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 175;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic=[tabelViewC.dataArray objectAtIndex:indexPath.row];
    InsuranceBrokersTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"InsuranceBrokersTableViewCell" forIndexPath:indexPath];
    [cell setValueForDictionary:dic indexPath:indexPath type:@"Experts"];
    return cell;
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [txtSarch resignFirstResponder];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *dic=[tabelViewC.dataArray objectAtIndex:indexPath.row];
    [self pushController:[InsuranceBrokersContentViewController class] withInfo:@"Insurance" withTitle:@"牌照详情" withOther:dic];
    
}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [txtSarch resignFirstResponder];
}
#pragma mark button
-(void)rightButtonClicked{
    [txtSarch resignFirstResponder];
    [self pushController:[UserSetViewController class] withInfo:@"" withTitle:@" " withOther:nil];
    
}
#pragma mark UITextFieldDelegate
-(BOOL)textFieldShouldReturn:(UITextField *)textField{    
    [self refreshTableData];
    [txtSarch resignFirstResponder];
    return YES;
}

#pragma mark ProductKeysViewDelegate
-(void)productKeysView:(ProductKeysView *)selectView clickeDic:(NSDictionary *)dic{
    txtSarch.text=[dic valueForJSONStrKey:@"txt"];
    [self refreshTableData];
    [txtSarch resignFirstResponder];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
